package core

import (
	"context"
	"log"
	"time"

	"server/config"

	"github.com/redis/go-redis/v9"
)

var (
	RDB = InitRedis()
	ctx = context.Background()
)

func InitRedis() *redis.Client {
	r := config.Config.Redis
	client := redis.NewClient(&redis.Options{
		Addr:     r.Host + ":" + r.Port,
		Password: r.Password,
		DB:       r.Database,
		PoolSize: r.Poll,
		MinIdleConns: r.Conn,
		DialTimeout:  5 * time.Second,
		ReadTimeout:  5 * time.Second,
		WriteTimeout: 5 * time.Second,
		PoolTimeout:  5 * time.Second,
	})

	_, err := client.Ping(ctx).Result()
	if err != nil {
		log.Fatalln("连接redis失败", err)
	}

	return client
}
